#include "User.h"
#include "Mesh.h"
#include "Core.h"

#include <dlfcn.h>
#include <string>

#define DLL_LOG_ERR printf("err %s\n", dlerror());

int exp(int a, int b)
{
    return 0;
};

typedef int CoreAdd(int, int);

void LoadDll()
{

    // RTLD_NOW
    // RT_GLOBAL
    // RT_LOCAL
    // RTLD_LAZY

    std::cout << add(100, 20) << std::endl;

    void *dllAddr = dlopen("../../CMakeDynamicLib/build/libtestDll.so", RTLD_NOW);
    if (dllAddr == nullptr)
    {
        printf("Dll addr is error");
        
    }
    printf("dll get from the path %p \n", dllAddr);
    void *addHandle = dlsym(dllAddr, "add");
    if (addHandle == nullptr)
    {
        printf("failed get the function add\n");
        return;

    }
    printf("Get a add func handle %p\n", addHandle);

    // decltype(exp) xs;
    CoreAdd *funcAddr = reinterpret_cast<CoreAdd*>(addHandle);

    int r= funcAddr(200, 200);
    printf("get a value %d \n",r);
    
    int res = dlclose(dllAddr);

    if (res!=0)
    {
        printf("close error \n");
    }

    printf("close successfully\n");
};

int main()
{

    LoadDll();

    Mesh::Desc desc;
    Mesh mesh;
    mesh.Create(desc);

    User user;

    user.children.emplace_back(User());
    user.children.emplace_back(User());
    user.children.emplace_back(User());
    // 完成
    // 生成动态库
    user.Process();
}